<template>
    <div class="white-box">
        <div class="task-one">
            <div class="task-one__item">
                <span class="task-one__label">业务模块：</span>
                <el-input
                    placeholder="请输入业务模块"
                    v-model="state.title"
                    @keyup.enter.native="handleSearch"
                    clearable
                >
                </el-input>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">业务类型：</span>
                <el-select
                    v-model="state.businessType"
                    placeholder="请选择业务类型"
                    clearable
                >
                    <el-option
                        v-for="item in state.businessTypeList"
                        :key="item.memberKey"
                        :label="item.memberName"
                        :value="item.memberKey"
                    />
                </el-select>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">用户ID：</span>
                <el-input
                    placeholder="请输入用户ID"
                    v-model="state.operatorUserId"
                    @keyup.enter.native="handleSearch"
                    clearable
                >
                </el-input>
            </div>
            <div class="task-one__btn clearfix">
                <el-button
                    type="primary"
                    :loading="loading"
                    icon="Search"
                    @click="handleSearch"
                    >搜索</el-button
                >
            </div>
        </div>

        <div>
            <el-button
                class="table-mb"
                v-hasPermi="['enttp:operationLog:del']"
                @click="onClickDel()"
                >批量删除操作日志</el-button
            >
        </div>

        <el-table
            class="table-mb"
            v-loading="loading"
            :data="arr_data"
            row-key="sid"
            :header-cell-style="{
                background: '#F5F7FA',
                textAlign: 'center',
            }"
            :cell-style="
                ({ column }) => {
                    return {
                        textAlign:
                            column.className === 'operation-nav'
                                ? 'left'
                                : 'center',
                        paddingLeft:
                            column.className === 'operation-nav' ? '25px' : '0',
                    };
                }
            "
            border
            element-loading-background="rgba(255, 255, 255, 0.6)"
            @selection-change="selectionChange"
            @sort-change="sortChange"
        >
            <el-table-column
                type="selection"
                width="55"
                v-hasPermi="['enttp:operationLog:del']"
            />
            <el-table-column
                prop="title"
                label="业务模块"
                min-width="250"
                fixed="left"
                sortable="custom"
                :show-overflow-tooltip="{
                    'popper-class': 'max-w-800',
                }"
            ></el-table-column>
            <el-table-column
                prop="businessType"
                label="业务类型"
                min-width="150"
                sortable="custom"
            >
                <template #default="{ row }">
                    {{
                        dictStore.getDictMemberName(
                            "business_type",
                            row.businessType
                        )
                    }}
                </template>
            </el-table-column>
            <el-table-column
                prop="operatorUserId"
                label="用户ID"
                min-width="150"
                sortable="custom"
            ></el-table-column>
            <el-table-column
                prop="operatorIp"
                label="用户IP"
                min-width="150"
                sortable="custom"
            ></el-table-column>
            <el-table-column label="操作类别" min-width="150">
                <template #default="{ row }">
                    {{
                        dictStore.getDictMemberName(
                            "operation_type",
                            row.operationType
                        )
                    }}
                </template>
            </el-table-column>
            <el-table-column
                prop="requestTime"
                label="操作时间"
                min-width="220"
                sortable="custom"
            ></el-table-column>
            <el-table-column
                label="操作"
                width="200"
                class-name="operation-nav"
                fixed="right"
            >
                <template #default="{ row }">
                    <MoreBtn
                        :buttons="[
                            {
                                props: {
                                    onClick: () => showDetail(row),
                                },
                                text: '详情',
                            },
                            {
                                props: {
                                    onClick: () => onClickDel(row.sid),
                                    'v-hasPermi': ['enttp:operationLog:del'],
                                },
                                text: '删除',
                            },
                        ]"
                    />
                </template>
            </el-table-column>
        </el-table>

        <el-pagination
            background
            v-model:current-page="page"
            v-model:page-size="pageSize"
            :page-sizes="pageSizeNum"
            :total="totalNums"
            layout="slot, sizes, prev, pager, next, jumper"
        >
            <span class="color-#606266"
                >共&nbsp;{{
                    totalNums >= 100 * pageSize
                        ? `${100 * pageSize}+`
                        : totalNums
                }}&nbsp;条</span
            >
        </el-pagination>
    </div>

    <el-drawer v-model="state.showDetail" title="日志详情" size="540">
        <template v-if="detail">
            <el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">业务名称</el-col>
                <el-col :span="18">{{ detail.title }}</el-col>
            </el-row>
            <el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">业务类型</el-col>
                <el-col :span="18">{{
                    dictStore.getDictMemberName(
                        "business_type",
                        detail.businessType
                    )
                }}</el-col>
            </el-row>
            <el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">操作者ID</el-col>
                <el-col :span="18">{{ detail.operatorUserId }}</el-col> </el-row
            ><el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">操作者IP</el-col>
                <el-col :span="18">{{ detail.operatorIp }}</el-col> </el-row
            ><el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">操作类别</el-col>
                <el-col :span="18">{{
                    dictStore.getDictMemberName(
                        "operation_type",
                        detail.operationType
                    )
                }}</el-col> </el-row
            ><el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">操作状态</el-col>
                <el-col :span="18">{{
                    dictStore.getDictMemberName(
                        "operation_status",
                        detail.status
                    )
                }}</el-col> </el-row
            ><el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">请求方式</el-col>
                <el-col :span="18">{{ detail.requestMethod }}</el-col>
            </el-row>
            <el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">请求路径</el-col>
                <el-col :span="18">{{ detail.requestUrl }}</el-col>
            </el-row>
            <el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">请求参数</el-col>
                <el-col :span="18" class="break-all">{{
                    detail.requestParam
                }}</el-col>
            </el-row>
            <el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">请求方法</el-col>
                <el-col :span="18" class="break-all">{{
                    detail.method
                }}</el-col>
            </el-row>
            <el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">返回参数</el-col>
                <el-col :span="18" class="break-all">{{
                    detail.result
                }}</el-col>
            </el-row>
            <el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">请求时间</el-col>
                <el-col :span="18">{{ detail.requestTime }}</el-col>
            </el-row>
            <el-row :gutter="20" class="mb-20">
                <el-col :span="6" align="end">消耗时长</el-col>
                <el-col :span="18">{{ detail.costTime }}毫秒</el-col>
            </el-row>
        </template></el-drawer
    >
</template>

<script setup>
import {
    inject,
    reactive,
    onMounted,
    onActivated,
    computed,
    toRefs,
} from "vue";
import OneLoad from "@mixins/one-load";
import MoreBtn from "@/components/MoreBtn.vue";
import { ElMessageBox } from "element-plus";
import { getOperateLogs, delOperateLogs } from "@/apis/system/log";
import { useDictStore } from "@stores/dict.js";

const dictStore = useDictStore();
dictStore.getDictMembers("operation_type");
dictStore.getDictMembers("operation_status");
dictStore
    .getDictMembers("business_type")
    .then((res) => (state.businessTypeList = res));

const $message = inject("$message");

const state = reactive({
    businessTypeList: [],
    ascended: false,
    sortFieldName: null,
    title: null,
    operatorUserId: null,
    businessType: null,
    sidList: [],
    showDetail: false,
    detail: null,
});
const { detail } = toRefs(state);

onActivated(() => {
    pubParams(getList);
});

/**
 * minxin列表加载
 */
const {
    loading,
    arr_data,
    pubRefreshParams,
    pubParams,
    page,
    pageSize,
    getdata,
    pageSizeNum,
    totalNums,
    openPaging,
} = OneLoad();

openPaging(getList);

function getList() {
    getdata(
        getOperateLogs({
            current: page.value,
            size: pageSize.value,
            ascended: state.ascended,
            sortFieldName: state.sortFieldName,
            title: state.title,
            operatorUserId: state.operatorUserId,
            businessType: state.businessType,
        })
    );
}

// 搜索
const handleSearch = () => {
    pubRefreshParams(getList);
};

// 排序
const sortChange = (e) => {
    const { property, order } = e.column;
    state.ascended = order === "ascending";
    state.sortFieldName = order ? property : null;
    pubRefreshParams(getList);
};

// 选中项改变
const selectionChange = (e) => {
    state.sidList = e.map((item) => item.sid);
};

const showDetail = (row) => {
    state.showDetail = true;
    state.detail = row;
};

// 删除
const onClickDel = async (sid) => {
    if (sid) {
        state.sidList = [sid];
    }
    if (!state.sidList.length) {
        return $message({ message: "请先勾选要删除的数据", type: "warning" });
    }
    await ElMessageBox.confirm("确定删除操作日志吗？");
    const res = await delOperateLogs({ sidList: state.sidList });
    if (res.data.code == 200) {
        $message({ message: "删除成功", type: "success" });
        pubParams(getList);
    } else {
        $message({ message: res.data.msg, type: "error" });
    }
};
</script>

<style lang="less" scoped></style>
